package test.hxy.mongoSchoolSimple.dao;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import test.hxy.mongoSchoolSimple.model.Campus;

@Repository
public class CampusTenplateDao {

    @Autowired
    MongoTemplate mongoTemplate;
    public void insert(Campus campus){
        mongoTemplate.insert(campus);
    }
    public void delete(String campusId) {
        Query query = new BasicQuery(new Document("campusId", campusId));
        mongoTemplate.remove(query, Campus.class);
    }
    public void updatePrincipal(String campusId, String administrator) {
        Query query = new Query();
        query.addCriteria(Criteria.where("campusId").is(campusId));
        Update update = Update.update("administrator", administrator);
        mongoTemplate.upsert(query, update, "campus");
    }
}
